package com.easy.work.db;

import android.content.Context;

import com.base.syslib.Logs;
import com.easy.work.app.AppSession;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * @author zhangfeng
 * @date 2018/01/07
 * @desc 消息数据库测试
 */

public class DataProvider {

    private static final String TAG = DataProvider.class.getSimpleName();

    private static final String[] msgText = {"系统消息", "面试消息", "录用消息"};

    public static void saveNewData(Context ctx, String jsonStr) {
        try {
            JSONObject object = new JSONObject(jsonStr);
            int msgType = object.optInt("msgType");
            Logs.e(TAG, "msgType:" + msgType);
            String title = object.optString("title");
            JSONObject o = object.optJSONObject("body");
            String companyName, date;
            MessageBean bean = new MessageBean();
            if (msgType == 3) {
                companyName = o.optString("content");
                date = o.optString("pushDate");
                bean.setAddress("");
                bean.setMaterial("");
                bean.setPhone("");
            } else {
                companyName = o.optString("companyName");
                date = o.optString("time");
                bean.setAddress(o.optString("address"));
                bean.setMaterial(o.optString("material"));
                bean.setPhone(o.optString("phone"));
            }

            // 模拟测试
//            int result = new Random().nextInt(2);
//            result++;
//            msgType = result;
//            Logs.e("JPush", "msgType:" + msgType);
//            companyName = "上海兴业公司";
//            date = "2017-10-10";
//            bean.setAddress("科学大道");
//            bean.setMaterial("身份证复印件");
//            bean.setPhone("1232423521");

            bean.setUserId(AppSession.getInstance().getUserId());
            bean.setReadState(false);
            bean.setMsgType(msgType);
            bean.setMsgTypeText(msgText[msgType % 3]);
            bean.setTitle(title);
            bean.setCompanyName(companyName);
            bean.setDate(date);
            if (date.contains(" ")) {
                date = date.split(" ")[0];
            }
            bean.setTime(date);
            MessageDao dao = new MessageDao(ctx);
            dao.add(bean);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public static void deleteData(Context ctx, MessageBean bean) {
        MessageDao dao = new MessageDao(ctx);
        dao.delete(bean);
    }

    public static List<MessageBean> getSystemMsg(Context ctx, boolean isRead) {
        MessageDao dao = new MessageDao(ctx);
        return dao.getSystemMsg(isRead);
    }

    public static List<MessageBean> queryByUserIdAndMxLy(Context ctx) {
        MessageDao dao = new MessageDao(ctx);
        return dao.queryByUserIdAndMxLy();
    }

    public static List<MessageBean> queryAllByUserId(Context ctx) {
        MessageDao dao = new MessageDao(ctx);
        return dao.queryAllByUserId();
    }

    public static List<MessageBean> queryByUserIdAndMsgType(Context ctx, int msgType) {
        MessageDao dao = new MessageDao(ctx);
        return dao.queryByUserIdAndMsgType(msgType);
    }

    public static void updateReadState(Context ctx, MessageBean bean) {
        MessageDao dao = new MessageDao(ctx);
        bean.setReadState(true);
        dao.update(bean);
    }

    public static void updateReadSystemMsg(Context ctx) {
        List<MessageBean> list = getSystemMsg(ctx, false);
        if (list != null && !list.isEmpty()) {
            MessageDao dao = new MessageDao(ctx);
            for (MessageBean bean : list) {
                bean.setReadState(true);
                dao.update(bean);
            }
        }
    }

    public static List<MessageBean> getUnreadMsgList(Context ctx) {
        MessageDao dao = new MessageDao(ctx);
        return dao.getUnreadMsgList();
    }

    /**
     * 默认生成一条系统消息
     */
    public static MessageBean getMsgInstance() {
        MessageBean bean = new MessageBean();
        bean.setTitle("易上班");
        bean.setCompanyName("欢迎使用易上班");
        bean.setMsgType(3);
        bean.setMsgTypeText("系统消息");
        bean.setReadState(true);
        bean.setTime(new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(new Date()));
        bean.setPhone("##");
        return bean;
    }
}
